package main;

import java.sql.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import server.ConnectionHandler;
import server.ServerConstants;
import simulation.Simulation;
import db.MessageDAO;
import db.MongoDBDAO;
import db.MySQLDAO;
import entities.Location;
import entities.Message;

public class Main {
	private enum Action {
		MONGO_INSERT_FIRST,
		MONGO_INSERT_SECOND,
		MONGO_RETRIEVE,
		MONGO_CLEAR,
		MYSQL_INSERT,
		MYSQL_RETRIEVE,
		MYSQL_CLEAR
	}

	private static Logger logger;;
	
	public static void startServer(MessageDAO db){
		ConnectionHandler connectionHandler = new ConnectionHandler(ServerConstants.PORT_NUMBER, db);
		
		Thread t = new Thread(connectionHandler);
		logger.info("Starting server");
		t.start();
		
		Simulation.sleep(300);
	}
	
	public static void main(String[] args) {
		startServer();
		// testDatabase();
	}
	
	public static void startServer() {
		logger = LoggerFactory.getLogger(Main.class);
		//MessageDAO db = new MongoDBDAO();
		MessageDAO db = MySQLDAO.getInstance();
		db.connect();
		db.clearDatabase();
		startServer(db);
	}
	
	public static void testDatabase() {
		Action testAction = Action.MONGO_RETRIEVE;
		// MONGO_INSERT_FIRST MONGO_INSERT_SECOND MONGO_RETRIEVE MONGO_CLEAR
		// MYSQL_INSERT MYSQL_RETRIEVE MYSQL_CLEAR
		
		MessageDAO db = null;
		
		switch (testAction) {
			case MONGO_INSERT_FIRST:
				db = new MongoDBDAO();
				db.connect();
				for (int i = 0; i < 5; i++) {
					db.addMessage(new Message(i, i * 10, new Location(i * 10.0, i * 10.0 + 10), new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() + 1000 * 3600), "Message number " + (i + 1)));
				}
				break;
			case MONGO_INSERT_SECOND:
				db = new MongoDBDAO();
				db.connect();
				for (int i = 0; i < 2; i++) {
					db.addMessage(new Message(100 + i, i * 10, new Location(i * 10.0, i * 10.0 + 10), new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() + 1000 * 3600), "EXTRA " + (i + 1)));
				}
				break;
			case MONGO_RETRIEVE:
				db = new MongoDBDAO();
				db.connect();
				List<Message> msgs = db.getMessagesForRecipientLocation(1, new Location(20, 20));
				for (Message msg : msgs) {
					System.out.println(msg);
				}
				break;
			case MONGO_CLEAR:
				db = new MongoDBDAO();
				db.connect();
				db.clearDatabase();
				break;
			case MYSQL_INSERT:
				db = MySQLDAO.getInstance();
				db.clearDatabase();
				for (int i = 0; i < 20; i++) {
					db.addMessage(new Message(i + 1, // ID
							(i + 1) * 10, // Sender ID
							new Location(15.0 + (10.0 * Math.random() - 5.0), 15.0 + (10.0 * Math.random() - 5.0)), // Location between 10.0 and 20.0
							new Date(System.currentTimeMillis()), // Date of origin
							new Date(System.currentTimeMillis()), // Date of expiry
							"Message with id = " + (i + 1) )
					);
				}
				break;
			case MYSQL_RETRIEVE:
				db = MySQLDAO.getInstance();		
				List<Message> messages = db.getMessagesForRecipientLocation(2, new Location(16.8, 18.8));
				System.out.println("List of all messages:");
				System.out.println("---------------------");
				for (Message message : messages) {
					System.out.println(message + "\n");
				}
				db.closeConnection();
				break;
			case MYSQL_CLEAR:
				db = MySQLDAO.getInstance();
				db.clearDatabase();
				break;
		}
	}
}